const RegistryDao = require('../dao/registry');
const ValidatorRegistry = require('../validators/registry');
const ok = require('../core/response-shortcut');
const { createJWT } = require('../core/jwt')
const {COOKIEJWTNAME}= require('../config/config.js');

class registryController {
    /**
     * 创建registryController
     * @param ctx
     * @returns {Promise.<void>}
     */
    static async createUser(ctx) {
        // 接收客户端参数
        let req = ctx.request.body;

        //验证器,如果验证不通过,直接在验证器里报错
        await ValidatorRegistry.createUser(req)

        //验证通过,操作数据库,创建user sql,
        //返回jwt值,因前后端分离,所以由前端设置cookie
        let encode_id = await RegistryDao.createUser(ctx);
        let jwtStr = createJWT({ 'id': encode_id })
        ctx.response.status = 200;
        ctx.body = ok.json({ [COOKIEJWTNAME]: jwtStr })
    }
}

module.exports = registryController
